﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MINERP.MODEL.FoundationModel;
using MINERP.DAL;
using MINERP.COM;

namespace MINERP.BLL.Statistics.NM
{
    public class NM_ShopBLL
    {
        /// <summary>
        /// 得到所有的商户列表
        /// </summary>
        /// <returns></returns>
        public List<Shop_Info> GetAllShop()
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                return ConvertToShop_InfoFromShop(db.Shop.Where(sh => sh.Status == 1).ToList());
            }
        }

        private List<Shop_Info> ConvertToShop_InfoFromShop(List<Shop> shopList)
        {
            List<Shop_Info> shopInfoList = new List<Shop_Info>();
            //shopInfoList.Add(new Shop_Info() {Address="全部",ShopName="全部",ShopId=0 });
            foreach (var shop in shopList)
            {
                shopInfoList.Add(new Shop_Info()
                {
                    ShopId = shop.ShopId,
                    ShopName = shop.shopName,
                    Area = shop.Area,
                    Salesman = shop.Salesman,
                    CheckOutType=shop.CheckOutType,
                     MnemonicCode=shop.mnemonicCode
                });

            }

            return shopInfoList;
        }


        /// <summary>
        /// 根据时间和影楼的ID得到这些影楼下的所有帐单
        /// </summary>
        /// <param name="selectedShopList"></param>
        /// <param name="dateTime"></param>
        /// <param name="dateTime_3"></param>
        /// <returns></returns>
        public List<SHOP_Bill_Info> GetShopMoeny(List<int> selectedShopList, DateTime dtStart, DateTime dtEnd, out List<Shop_Info> outShop, bool onlyshowArrearsShop)
        {
            List<SHOP_Bill_Info> sbInfoList = new List<SHOP_Bill_Info>();
            outShop = new List<Shop_Info>();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                foreach (var shopId in selectedShopList)
                {
                    int delStatus = (int)ShopBillStatus.Delete;
                    int uncheckStatus = (int)ShopBillStatus.UnCheckOut;
                    List<SHOP_Bill> sbList = db.SHOP_Bill.Where(b => b.Shop.ShopId == shopId && b.CreateTime >= dtStart && b.CreateTime <= dtEnd && b.Status != delStatus).ToList();
                    Shop shop = db.Shop.FirstOrDefault(s=>s.ShopId ==shopId);
                    if (onlyshowArrearsShop)
                    {
                        int billStatus =(int)ShopBillStatus.UnCheckOut;
                        if (shop.Balance >= 0&&sbList.Where(s=>s.Status ==billStatus).ToList().Count<1)//账户余额大于零并且没有账单为欠款状态
                        {
                            continue;
                        }
 
                    }
                    if (sbList.Count == 0)
                    {
                        //Shop shop = db.Shop.FirstOrDefault(p => p.ShopId == shopId);

                        //enPeoductShopList.Add(new Shop_Info()
                        //{
                        //    Address = shop.address,
                        //    Balance = shop.Balance ?? 0,
                        //    Remark = shop.Remark,
                        //    Area = shop.Area,
                        //    CheckOutType = shop.CheckOutType,
                        //    CreditLevel = shop.CreditLevel ?? 0,
                        //    Tel1 = shop.Tel1,
                        //    Tel2 = shop.Tel2,
                        //    CreditLimit = shop.CreditLimit ?? 0,
                        //    EmailAddr = shop.EmailAddr,
                        //    IsNeedPrintUPrice = shop.IsNeedPrintUPrice ?? false,
                        //    QQ = shop.QQ,
                        //    MSN = shop.MSN,
                        //    Priority = shop.Priority ?? 0,
                        //    ShopName = shop.shopName,
                        //    ShopId = shop.ShopId,
                        //    Status = shop.Status ?? 0,
                        //    Salesman = shop.Salesman,
                        //    OrderBalance = shop.OrderBalance ?? 0
                        //});

                        //获取商户未结账的账单
                        sbList = db.SHOP_Bill.Where(b => b.Shop.ShopId == shopId && b.Status == uncheckStatus).ToList();
                        if (sbList.Count == 0)
                        {
                            outShop.Add(new Shop_Info()
                        {
                            Address = shop.address,
                            Balance = shop.Balance ?? 0,
                            Remark = shop.Remark,
                            Area = shop.Area,
                            CheckOutType = shop.CheckOutType,
                            CreditLevel = shop.CreditLevel ?? 0,
                            Tel1 = shop.Tel1,
                            Tel2 = shop.Tel2,
                            CreditLimit = shop.CreditLimit ?? 0,
                            EmailAddr = shop.EmailAddr,
                            IsNeedPrintUPrice = shop.IsNeedPrintUPrice ?? false,
                            QQ = shop.QQ,
                            MSN = shop.MSN,
                            Priority = shop.Priority ?? 0,
                            ShopName = shop.shopName,
                            ShopId = shop.ShopId,
                            Status = shop.Status ?? 0,
                            Salesman = shop.Salesman,
                            OrderBalance = shop.OrderBalance ?? 0
                        });
                        }
                    }
                    foreach (var sb in sbList)
                    {
                        //一个个地帐单
                        if (!sb.ShopReference.IsLoaded)
                        {
                            sb.ShopReference.Load();
                        }
                        SHOP_Bill_Info sbinfo = new SHOP_Bill_Info()
                        {
                            BillCount = Convert.ToDouble(sb.TotalPrice ?? 0),
                            BillId = sb.BillId,
                            BillOrderCode = sb.BillOrderCode,
                            CreateTime = sb.CreateTime,
                            List_Detail = new List<Bill_Detail_Info>(),
                            ShopBalance = Convert.ToDouble(sb.ShopBalance),
                            Status = sb.Status
                        };

                        sbinfo.Shop = new Shop_Info()
                        {
                            Address = sb.Shop.address,
                            Balance = sb.Shop.Balance ?? 0,
                            Remark = sb.Shop.Remark,
                            Area = sb.Shop.Area,
                            CheckOutType = sb.Shop.CheckOutType,
                            CreditLevel = sb.Shop.CreditLevel ?? 0,
                            Tel1 = sb.Shop.Tel1,
                            Tel2 = sb.Shop.Tel2,
                            CreditLimit = sb.Shop.CreditLimit ?? 0,
                            EmailAddr = sb.Shop.EmailAddr,
                            IsNeedPrintUPrice = sb.Shop.IsNeedPrintUPrice ?? false,
                            QQ = sb.Shop.QQ,
                            MSN = sb.Shop.MSN,
                            Priority = sb.Shop.Priority ?? 0,
                            ShopName = sb.Shop.shopName,
                            ShopId = sb.Shop.ShopId,
                            Status = sb.Shop.Status ?? 0,
                            Salesman = sb.Shop.Salesman,
                            OrderBalance = sb.Shop.OrderBalance ?? 0
                        };


                        if (!sb.Bill_Detail.IsLoaded)
                        {
                            sb.Bill_Detail.Load();
                        }
                        foreach (var bd in sb.Bill_Detail.ToList())
                        {
                            //产品
                            Bill_Detail_Info bdetailInfo = new Bill_Detail_Info()
                            {
                                BillDetailId = bd.BillDetailId,
                                CtmName = bd.CtmName,
                                OrderInfoCode = bd.OrderInfoCode,
                                OrderProductName = bd.OrderProductName,
                                Remark = bd.OiRemark,
                                ListDetail_Detail = new List<Bill_Detail_Detail_Info>()
                            };


                            //加明细
                            if (!bd.Bill_Detail_Detail.IsLoaded)
                            {
                                bd.Bill_Detail_Detail.Load();
                            }
                            foreach (var bdd in bd.Bill_Detail_Detail.ToList())
                            {
                                bdetailInfo.ListDetail_Detail.Add(new Bill_Detail_Detail_Info()
                                {
                                    Area = bdd.Area,
                                    Count = bdd.Count,
                                    DetailName = bdd.DetailName,
                                    Num = bdd.Num,
                                    Spec = bdd.Spec,
                                    Type = bdd.Type ?? 0,
                                    UnitPrice = bdd.UnitPrice,
                                    DetailRemark =bdd.DetailRemark
                                });
                            }

                            sbinfo.List_Detail.Add(bdetailInfo);
                        };
                        sbInfoList.Add(sbinfo);
                    }
                }
            }

            return sbInfoList;
        }

        /// <summary>
        /// 查看某一商户一段时间内的资金流程记录表，并以发生时间排序
        /// </summary>
        /// <param name="shopID"></param>
        /// <param name="dateTime"></param>
        /// <param name="dateTime_3"></param>
        /// <returns></returns>
        public List<ShopBlanceCrushRecord_Info> GetShopCrushRecord(int shopID, DateTime dtStartTime, DateTime dtEndTime)
        {
            List<ShopBlanceCrushRecord_Info> resultList = new List<ShopBlanceCrushRecord_Info>();

            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                foreach (var s in db.ShopBlanceCrushRecord.Where(sc => sc.Shop.ShopId == shopID && sc.CrushTime >= dtStartTime && sc.CrushTime <= dtEndTime))
                {
                    resultList.Add(new ShopBlanceCrushRecord_Info()
                    {
                        CrushMoney = Convert.ToDouble(s.CrushMoney),
                        CrushTime = s.CrushTime,
                        Operation = s.Operation ?? 0,
                        Operator = s.Operator,
                        Overage = Convert.ToDouble(s.Overage),
                        Remark = s.Remark,
                        ShopID = shopID,
                         ShopBlanceCrushRecordId =s.ShopBlanceCrushRecordId
                         

                    });
                }
            }

            return resultList.OrderBy(c => c.ShopBlanceCrushRecordId).ToList();
        }
        /// <summary>
        /// 查看某一商户一段时间内的资金流程记录表，并以发生时间排序
        /// </summary>
        /// <param name="shopID"></param>
        /// <param name="dateTime"></param>
        /// <param name="dateTime_3"></param>
        /// <returns></returns>
        public List<ShopBlanceCrushRecord_Info> GetShopCrushChongRecord(int shopID, DateTime dtStartTime, DateTime dtEndTime)
        {
            List<ShopBlanceCrushRecord_Info> resultList = new List<ShopBlanceCrushRecord_Info>();

            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                foreach (var s in db.ShopBlanceCrushRecord.Where(sc => sc.Shop.ShopId == shopID && sc.CrushTime >= dtStartTime && sc.CrushTime <= dtEndTime && sc.Operation == 0))
                {
                    resultList.Add(new ShopBlanceCrushRecord_Info()
                    {
                        CrushMoney = Convert.ToDouble(s.CrushMoney),
                        CrushTime = s.CrushTime,
                        Operation = s.Operation ?? 0,
                        Operator = s.Operator,
                        Overage = Convert.ToDouble(s.Overage),
                        Remark = s.Remark,
                        ShopID = shopID

                    });
                }
            }

            return resultList.OrderBy(c => c.CrushTime).ToList();
        }
        /// <summary>
        /// 查看某一商户一段时间内的资金流程记录表，并以发生时间排序
        /// </summary>
        /// <param name="shopID"></param>
        /// <param name="dateTime"></param>
        /// <param name="dateTime_3"></param>
        /// <returns></returns>
        public ShopBlanceCrushRecord_Info GetShopLastCrushRecord(int shopID, DateTime dtStartTime, DateTime dtEndTime)
        {
            ShopBlanceCrushRecord_Info result = new ShopBlanceCrushRecord_Info();

            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                ShopBlanceCrushRecord sbcrLast = db.ShopBlanceCrushRecord.Where(sc => sc.Shop.ShopId == shopID && sc.CrushTime >= dtStartTime && sc.CrushTime <= dtEndTime).ToList().OrderBy(pt => pt.ShopBlanceCrushRecordId).LastOrDefault();
                //foreach (var s in db.ShopBlanceCrushRecord.Where(sc => sc.Shop.ShopId == shopID && sc.CrushTime >= dtStartTime && sc.CrushTime <= dtEndTime ))
                //{
                //    resultList.Add(new ShopBlanceCrushRecord_Info()
                //    {
                //        CrushMoney = Convert.ToDouble(s.CrushMoney),
                //        CrushTime = s.CrushTime,
                //        Operation = s.Operation ?? 0,
                //        Operator = s.Operator,
                //        Overage = Convert.ToDouble(s.Overage),
                //        Remark = s.Remark,
                //        ShopID = shopID

                //    });
                //}
                if (sbcrLast != null)
                {
                    result = new ShopBlanceCrushRecord_Info()
                        {
                            CrushMoney = Convert.ToDouble(sbcrLast.CrushMoney),
                            CrushTime = sbcrLast.CrushTime,
                            Operation = sbcrLast.Operation ?? 0,
                            Operator = sbcrLast.Operator,
                            Overage = Convert.ToDouble(sbcrLast.Overage),
                            Remark = sbcrLast.Remark,
                            ShopID = shopID

                        };
                }
                else
                {
                    result = null;
                }

                
            }

            return result;
        }
        /// <summary>
        /// 当且仅仅当时间范围内仅存在充值时返回最后一条数据
        /// </summary>
        /// <param name="shopID"></param>
        /// <param name="dateTime"></param>
        /// <param name="dateTime_3"></param>
        /// <returns></returns>
        public ShopBlanceCrushRecord_Info GetShopLastCrushRecord2(int shopID, DateTime dtStartTime, DateTime dtEndTime)
        {
            ShopBlanceCrushRecord_Info result = new ShopBlanceCrushRecord_Info();
            int crushMoney = (int) CountOperationType.ChongZhi;
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                ShopBlanceCrushRecord sbcrLast = new ShopBlanceCrushRecord();
                List<ShopBlanceCrushRecord> listRe =
                    db.ShopBlanceCrushRecord.Where(
                            sc => sc.Shop.ShopId == shopID && sc.CrushTime >= dtStartTime && sc.CrushTime <= dtEndTime)
                        .ToList()
                        .OrderBy(pt => pt.CrushTime).ToList();
                if (listRe.Count(s => s.Operation == crushMoney) == listRe.Count)
                {
                    sbcrLast = listRe.LastOrDefault();
                }
                else
                {
                    sbcrLast = null;
                }


                if (sbcrLast != null)
                {
                    result = new ShopBlanceCrushRecord_Info()
                    {
                        CrushMoney = Convert.ToDouble(sbcrLast.CrushMoney),
                        CrushTime = sbcrLast.CrushTime,
                        Operation = sbcrLast.Operation ?? 0,
                        Operator = sbcrLast.Operator,
                        Overage = Convert.ToDouble(sbcrLast.Overage),
                        Remark = sbcrLast.Remark,
                        ShopID = shopID

                    };
                }
                else
                {
                    result = null;
                }


            }

            return result;
        }
    }
}
